package com.yao.club.controller;

import com.yao.club.entity.dto.Message;
import com.yao.club.entity.request.AddMessageRequest;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.service.MessageService;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/app/message")
@Api(value = "消息模块", description = "消息模块", tags = "消息模块")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Resource
    HttpServletRequest request;

    @PostMapping("/addMessage")
    @ApiOperation(value = "添加消息", notes = "添加系统消息", httpMethod = "POST")
    public ResponseEntity<Boolean> addMessage(@RequestBody
                                              @ApiParam(value = "添加消息请求实体", required = true)
                                              AddMessageRequest request) {
        return messageService.addMessage(request);
    }

    @GetMapping("/latestMessages")
    @ApiOperation(value = "消息中心", notes = "消息中心", httpMethod = "GET")
    public ResponseEntity<List<Message>> getLatestMessagesByModule() {
        String username = (String) request.getAttribute("username");
        return messageService.getLatestMessagesByModule(username);
    }
    @GetMapping("/readMessage")
    @ApiOperation(value = "阅读消息", notes = "根据模块读取消息并标记为已读", httpMethod = "GET")
    public ResponseEntity<List<Message>> readMessage(@RequestParam("module") int module) {
        String username = (String) request.getAttribute("username");
        if (username == null) {
            return ResponseUtils.failed("用户未登录");
        }
        return messageService.readMessagesByModule(username, module);
    }
    @GetMapping("/messagesByModule")
    @ApiOperation(value = "分页查询消息", notes = "根据模块分页查询当前用户的所有消息", httpMethod = "GET")
    public ResponseEntity<List<Message>> getMessagesByModule(
            @RequestParam("module") int module,
            @RequestParam("page") int page,
            @RequestParam("size") int size) {
        String username = (String) request.getAttribute("username");
        if (username == null) {
            return ResponseUtils.failed("用户未登录");
        }
        return messageService.getMessagesByModule(username, module, page, size);
    }
}
